package com.lagou.edu.transactional;

import org.springframework.stereotype.Component;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@Component
public class MyJdbcTemplate {
    @Autowired
    private MyTransactionManager transactionManager;


    public void execute(String sql,@Nullable Object... args) throws SQLException {
        Connection connection = transactionManager.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < args.length; i++) {
            if(args[i] instanceof Integer){
                preparedStatement.setInt(i+1, (Integer) args[i]);
            }else if(args[i] instanceof String){
                preparedStatement.setString(i+1, (String) args[i]);
            }
        }
        preparedStatement.execute();


    }
}
